package queuedemo;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 黎鹤舞
 * Date: 2024-01-17
 * Time: 14:10
 */

//利用队列模拟实现栈
class MyStack {

    Queue<Integer> queue1;
    Queue<Integer> queue2;

    //构造函数
    public MyStack() {

        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }


    public void push(int x) {
        //显示queue2接收入栈的元素:
        queue2.offer(x);
        //如果queue1不为空，需要把queue1的内容逐个offer到queue2中
        while(!queue1.isEmpty()) {
            queue2.offer(queue1.poll());
        }
        //把queue1和queue2进行交换
        Queue<Integer> temp = queue1;
        queue1 = queue2;
        queue2 = temp;
    }

    public int pop() {
        return queue1.poll();
    }

    public int top() {
        return queue1.peek();
    }

    public boolean empty() {
        return queue1.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */